using GetTogether.Studio;
using System;
using System.Collections.Generic;
using System.Web;

namespace GetTogether.Studio.Schedule
{
    public class ScheduleService
    {
        public ScheduleService() { }

        public List<GetTogether.Studio.Schedule.ScheduleTask> GetSchedules(List<string> invokingScheduleNames)
        {
            List<GetTogether.Studio.Schedule.ScheduleTask> ss = GetTogether.Studio.Schedule.ScheduleTask.GetSchedules();
            List<GetTogether.Studio.Schedule.ScheduleTask> result = new List<GetTogether.Studio.Schedule.ScheduleTask>();
            int currentWeekValue = GetTogether.Utility.DateHelper.DayOfWeekToValue(DateTime.Now.DayOfWeek);
            string basePath = GetTogether.Studio.ProjectParameter.GetSettingsPathPublic(ProjectParameter.ProjectCategory.Schedule);
            if (invokingScheduleNames == null) invokingScheduleNames = new List<string>();
            foreach (GetTogether.Studio.Schedule.ScheduleTask s in ss)
            {
                string filePath = string.Concat(basePath, s.Name);
                if (invokingScheduleNames.Contains(s.Name))
                {
                    s.IsInvoking = true;
                    System.IO.File.WriteAllText(filePath, s.ToXml());
                    continue;
                }
                else if (s.IsInvoking && !invokingScheduleNames.Contains(s.Name))
                {
                    s.IsInvoking = false;
                    System.IO.File.WriteAllText(filePath, s.ToXml());
                    continue;
                }
                else if (s.ScheduleDateTime.Year > 1 && s.ScheduleDateTime.Year != DateTime.Now.Year) continue;
                else if (s.ScheduleDateTime.Month > 1 && s.ScheduleDateTime.Month != DateTime.Now.Month) continue;
                else if (s.ScheduleDateTime.Day > 1 && s.ScheduleDateTime.Day != DateTime.Now.Day) continue;

                bool isMatch = false;
                TimeSpan time = s.ScheduleDateTime.TimeOfDay;

                if (s.Status == GetTogether.Studio.Schedule.ScheduleStatus.Enabled
                    && (s.Week & currentWeekValue) == currentWeekValue
                    && DateTime.Now.Subtract(s.ActionOn).TotalMinutes > time.TotalMinutes)
                {
                    if (s.RepeatForTime)
                    {
                        if (s.ScheduleDateTime.Year > 1 || s.ScheduleDateTime.Month > 1 || s.ScheduleDateTime.Day > 1)
                        {
                            if (DateTime.Now.Date != s.ActionOn.Date)
                                isMatch = true;
                        }
                        else
                            isMatch = true;
                    }
                    else if (time <= DateTime.Now.TimeOfDay && s.ActionOn.Date != DateTime.Now.Date)
                    {
                        isMatch = true;
                    }
                }

                if (isMatch)
                {
                    s.ActionOn = DateTime.Now;
                    s.IsInvoking = true;
                    System.IO.File.WriteAllText(filePath, s.ToXml());
                    GetTogether.Studio.ProjectHistory.SaveHistory(ProjectParameter.ProjectCategory.Schedule, s.Name, "");
                    result.Add(s);
                }
            }
            return result;
        }

        public void UpdateSchedule(string scheduleName, string error, string response)
        {
            try
            {
                GetTogether.Studio.Schedule.ScheduleTask s = GetTogether.Studio.Schedule.ScheduleTask.GetScheduleByName(scheduleName);
                if (s == null) return;
                s.IsInvoking = false;
                GetTogether.Studio.Schedule.ScheduleHistoryList.Save(s, error, response);
                string filePath = string.Concat(GetTogether.Studio.ProjectParameter.GetSettingsPathPublic(ProjectParameter.ProjectCategory.Schedule), scheduleName);
                string scheduleXml = s.ToXml();
                System.IO.File.WriteAllText(filePath, scheduleXml);
                if (s.IsSendReport)
                {
                    string subject = string.Concat("Studio Schedule Report > ", s.Name);
                    string content = string.Empty;
                    if (!string.IsNullOrEmpty(error))
                    {
                        subject = string.Concat(subject, ",Error Occurred");
                        content = string.Concat("Error:", error, "\r\n\r\n");
                    }
                    content = string.Concat(content, "Report:\r\n", response);
                    content = string.Concat(content, "\r\n\r\nSchedule:\r\n", scheduleXml);
                    GetTogether.Studio.Functions.EmailHelper.SendEmail(GetTogether.Studio.Config.Original.ReporterEmail, GetTogether.Studio.Config.RecipientEmails, subject, content, false);
                }
            }
            catch (Exception ex)
            {
                GetTogether.Studio.Log.Error(ex);
            }
        }
    }
}